@model Netnr.Domain.Gist

@{
    ViewData["Title"] = Model.GistRemark;
}

<div class="container-fluid">
    <div class="row cp-header">
        <div class="container">
            <div class="row mt-3">
                <div class="col-12 text-right">
                    <div class="float-left">
                        <i class="fa fa-user fa-fw fa-2x text-muted mr-sm-2"></i>
                    </div>
                    <div class="float-left text-left">
                        <a href="/gist/user/@Model.Uid">netnr</a>&nbsp;/
                        <a href="/gist/code/@Model.GistCode"><b>@Model.GistFilename</b></a>
                        <div>
                            <small class="text-muted">@Model.GistCreateTime.Value.ToString("yyyy-MM-dd HH:mm")</small>
                        </div>
                    </div>

                    @{
                        var hc = ViewContext.HttpContext;
                        bool IsLogin = hc.User.Identity.IsAuthenticated;
                        var uinfo = new UserAuthAid(ViewContext.HttpContext).Get();
                    }
                    @if (IsLogin && Model.Uid == uinfo.UserId)
                    {
                        <a class="btn btn-outline-secondary btn-sm mr-sm-2" href="/gist/code/@Model.GistCode/edit">
                            <i class="fa fa-pencil"></i>&nbsp;Edit
                        </a>
                        <a class="btn btn-outline-danger btn-sm" href="/gist/code/@Model.GistCode/delete" onclick="return confirm('Are you positive you want to delete this Gist?')">
                            <i class="fa fa-trash-o"></i>&nbsp;Delete
                        </a>
                    }
                </div>
            </div>
            <div class="row mb-2">
                <div class="col-md-7"></div>
                <div class="col-md-5 col-sm-12">
                    <div class="input-group input-group-sm mt-3">
                        <div class="input-group-prepend">
                            <select class="btn btn-secondary" id="segistc">
                                <option value="Share">Share</option>
                            </select>
                        </div>
                        <input type="text" class="form-control" readonly="readonly" id="txtgistc">
                        <div class="input-group-prepend" id="copygistc">
                            <span class="btn btn-secondary fa fa-copy"></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="container" id="fileview">
            <div class="row">
                <div class="col-md-12 mt-4 mb-3">
                    @Model.GistRemark
                </div>
                <div class="col-md-12">
                    <div class="card gc-viewcode">
                        <div class="card-header">
                            <i class="fa fa-fw fa-spinner fa-spin" id="faicon"></i>&nbsp;
                            <a href="#fileview">@Model.GistFilename</a>
                            <small class="ml-3 badge badge-secondary">@Model.GistLanguage</small>
                            <div class="btn-group float-right">
                                <a href="/gist/raw/@Model.GistCode" class="btn btn-outline-secondary">Raw</a>
                                <a href="/gist/raw/@Model.GistCode/download" class="btn btn-outline-secondary">Download</a>
                            </div>
                        </div>
                        @if (Model.GistLanguage == "markdown")
                        {
                            <partial name="_PartialQuote" model="@("netnrmd.css,netnrmd.js,monaco-editor")" />

                            <div class="markdown-body px-2 py-2">@Model.GistContent</div>
                            <script>
                                var nmd = $('div.markdown-body');

                                nmd.html(DOMPurify.sanitize(marked(nmd.html(), {
                                    headerIds: false,
                                    highlight: function (str, lang) {
                                        if (window.hljs && hljs.getLanguage(lang)) {
                                            try {
                                                return hljs.highlight(lang, str).value;
                                            } catch (__) { }
                                        }
                                        try {
                                            return hljs.highlightAuto(str).value;
                                        } catch (__) { }
                                        return '';
                                    }
                                })));
                                $('#faicon').removeClass('fa-spinner fa-spin').addClass('fa-file-code-o');
                            </script>
                        }
                        else
                        {
                            <pre class="card-body monaco-editor vcln markdown-body" style="height:@((19*Model.GistRow+15).ToString()+"px");overflow-y:hidden;padding-left:65px;" data-lang="@Model.GistLanguage" data-theme="@Model.GistTheme">@Model.GistContent</pre>
                            <partial name="_PartialQuote" model="@("monaco-editor")" />

                            <script>
                                var editor = null;
                                require(['vs/editor/editor.main'], function () {
                                    var gc = $('pre.vcln');
                                    var cv = htmlDecodeByRegExp(gc.html());
                                    gc.html('');
                                    editor = monaco.editor.create(gc[0], {
                                        value: cv,
                                        language: gc.attr('data-lang'),
                                        automaticLayout: true,
                                        roundedSelection: false,
                                        scrollBeyondLastLine: false,
                                        contextmenu: false,
                                        scrollbar: {
                                            verticalScrollbarSize: 0,
                                            horizontalScrollbarSize: 9
                                        },
                                        readOnly: true,
                                        theme: gc.attr('data-theme'),
                                        minimap: {
                                            enabled: false
                                        }
                                    });
                                    gc.css('padding-left', 0);
                                    $('#faicon').removeClass('fa-spinner fa-spin').addClass('fa-file-code-o');
                                });
                            </script>
                        }
                    </div>
                </div>

                <!--同步镜像-->
                <div class="col-md-12 mt-3">
                    @if (!string.IsNullOrWhiteSpace(Model.Spare1))
                    {
                        <div title="GitHub 镜像">
                            <i class="fa fa-fw fa-github"></i>
                            <a target="_blank" href="https://gist.github.com/netnr/@Model.Spare1">https://gist.github.com/netnr/@Model.Spare1</a>
                        </div>
                    }
                    @if (!string.IsNullOrWhiteSpace(Model.Spare2))
                    {
                        <div title="Gitee 镜像">
                            <i class="fa fa-fw fa-git"></i>
                            <a target="_blank" href="https://gitee.com/netnr/codes/@Model.Spare2">https://gitee.com/netnr/codes/@Model.Spare2</a>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var sciarr = [
        location.href
    ];
    $('#segistc').change(function () {
        $('#txtgistc').val(sciarr[this.selectedIndex]);
    }).parent().next().val(sciarr[0]);
    $('#txtgistc').on('focus click', function () {
        this.focus();
        this.select();
    }).val(sciarr[0]);

    $('#copygistc').click(function () {
        $('#txtgistc')[0].focus();
        $('#txtgistc')[0].select();
    });

    function htmlDecodeByRegExp(str) {
        if (str.length == 0) return "";
        return str.replace(/&lt;/g, "<")
            .replace(/&gt;/g, ">")
            .replace(/&nbsp;/g, " ")
            .replace(/&#39;/g, "\'")
            .replace(/&quot;/g, "\"")
            .replace(/&amp;/g, "&");
    }
</script>